﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using MaQiu.SqlExecute.DAL;
using MaQiu.SqlExecute.DbProvider;
using MLogHelper;
using MLogHelper.Business;
using static MLogHelper.Business.Configurer;

namespace MLogHelper
{
    public partial class LogOperation : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!StaticInit.IsInit)
                {
                    StaticInit.Init();
                }
                //
                string type = Request["type"].ToString();
                string responseText = string.Empty;
                JavaScriptSerializer js = new JavaScriptSerializer();
                SqlConnection.InitSqlExecute(StaticInit.ConnectionString, new SqlServerProvider());
                try
                {
                    switch (type)
                    {
                        case "GetAllLogger":
                            responseText = js.Serialize(new Configurer().GetAllLogger());
                            break;
                        case "SetLoggerStatus":
                            new Configurer().SetLoggerStatus(Decode(Request["name"]), bool.Parse(Request["isOpen"]));
                            break;
                        case "DelLogger":
                            new Configurer().DelLogger(Decode(Request["name"]));
                            break;
                        case "EditLogger":
                            new Configurer().EditLogger(js.Deserialize<LoggerConfig>(Decode(Request["log"])));
                            break;
                        case "GetRecentLog":
                            responseText = js.Serialize(new Configurer().GetRecentLog(js.Deserialize<SearchRecentConfig>(Decode(Request["config"]))));
                            break;
                        case "SearchTextInLog":
                            responseText = js.Serialize(new Configurer().SearchTextInLog(js.Deserialize<SearchTextConfig>(Decode(Request["config"])), bool.Parse(Request["isSimple"])));
                            break;
                        case "AddLogger":
                            new Configurer().AddLogger(js.Deserialize<LoggerConfig>(Decode(Request["log"])));
                            break;
                    }
                }
                catch (ArgumentException ex)
                {
                    responseText = ex.Message;
                    Response.StatusCode = 500;
                }

                Response.Clear();
                Response.AddHeader("Access-Control-Allow-Origin", "*");
                Response.Write(responseText);
            }
            catch (Exception ex)
            {
                StaticInit.LogSimple(ex.ToString());
                throw ex;
            }
            finally
            {
            }
        }
        private string Decode(string txt)
        {
            return HttpUtility.UrlDecode(Encoding.UTF8.GetString(Convert.FromBase64String(txt)), Encoding.UTF8);
        }
    }
}